#!/usr/bin/env python
# -*- coding: utf-8 -*-

# save a csv file to db preparation
# needs to organize thie input file
# with the same order as db columns
import os
import subprocess

fin = "in.csv"
fout = "out.csv"

db_column_list = ["Tom", "Alice", "Mary", "Jim"]
db_column_index_list = []

#lines = open(fin, 'r').read().splitlines()
#print lines

with open(fin, 'r') as f:
    line = f.readline().rstrip('\n')
    items = line.split(',')
    for c in db_column_list:
        db_column_index_list.append(items.index(c) + 1)

awk_format = ",".join(["$%d"]*len(db_column_list))
awk_fields = awk_format % tuple(db_column_index_list)
cmd = "awk -F, '{print %s}' OFS=\",\" %s > %s" % (awk_fields, fin, fout)
subprocess.call(cmd, shell=True)

